package com.myway.service.ibe.command;

import com.alibaba.fastjson.JSON;
import com.myway.dto.ibe_param.FlightParam;
import com.myway.dto.ibe_param.TaxPrice;
import com.myway.dto.ibe_param.TicketPrice;
import com.myway.service.ibe.FareCommandService;
import com.travelsky.ebuild.clientapi.axi.*;
import com.travelsky.util.QDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

/**
 * Created by yufei on 2018/6/4.
 */
public class FareCommand extends BaseIBECommand implements FareCommandService {
    public List<TicketPrice> execute(List<FlightParam> params) {
        try {

            Pricing pricing = new Pricing();
            POSType posType = this.configIBEClientPrice(pricing);
            PITravelerInfo piTravelerInfo = new PITravelerInfo();

            Vector<PIPassenger> vector1 = new Vector<PIPassenger>();
            PIPassenger piPassenger1 = new PIPassenger();
//            piPassenger1.setAge(18);
            piPassenger1.setPassengerType(PIPassenger.ADULT);
            piPassenger1.setQuantity(params.get(0).getAdult());
            vector1.add(piPassenger1);
            if (params.get(0).getChild() > 0){
                PIPassenger piPassenger2 = new PIPassenger();
                piPassenger2.setAge(6);
                piPassenger2.setPassengerType(PIPassenger.CHILD);
                piPassenger2.setQuantity(params.get(0).getChild());
                vector1.add(piPassenger2);
            }else{
                PIPassenger piPassenger2 = new PIPassenger();
                piPassenger2.setAge(6);
                piPassenger2.setPassengerType(PIPassenger.CHILD);
                piPassenger2.setQuantity(1);
                vector1.add(piPassenger2);
            }
            if (params.get(0).getInfant() > 0){
                PIPassenger piPassenger3 = new PIPassenger();
                piPassenger3.setAge(1);
                piPassenger3.setPassengerType(PIPassenger.INFANT);
                piPassenger3.setQuantity(params.get(0).getInfant());
                vector1.add(piPassenger3);
            }else{
                PIPassenger piPassenger3 = new PIPassenger();
                piPassenger3.setAge(1);
                piPassenger3.setPassengerType(PIPassenger.INFANT);
                piPassenger3.setQuantity(1);
                vector1.add(piPassenger3);
            }

            piTravelerInfo.setPassengers(vector1);
            PricingRequest pricingRequest = new PricingRequest();
            pricingRequest.setPos(posType);
            pricingRequest.setReprice(false);
            pricingRequest.setAirTravelerAvail(piTravelerInfo);
            pricingRequest.setNegotiatedFareCode(null);
            pricingRequest.setDoNotIncludeFBCInHFC(false);
            pricingRequest.setIncludeNegotiatedFares(true);
            pricingRequest.setMaximumResponses(20);
            pricingRequest.setETicketIndicator(true);
            pricingRequest.setPricingSource(3);


            Vector vector = new Vector();
            //航段1
            String currency ="";
            String airline = "";
            for(int i = 0; i<params.size(); i++){
                PIFlightSegment segment = new PIFlightSegment();
                segment.setDepartureAirport(params.get(i).getOrgCity());
                segment.setArrivalAirport(params.get(i).getDstCity());
                Date arrtime_date = QDateTime.stringToDate(params.get(i).getFlightDate(), "yyyy-mm-ddThh:mi:ss");
                Date deptime_date = QDateTime.stringToDate(params.get(i).getArrDate(), "yyyy-mm-ddThh:mi:ss");
                segment.setArrivalDateTime(arrtime_date);
                segment.setDepartureDateTime(deptime_date);
                segment.setStatus(PIFlightSegment.SOLD_STATUS);
                segment.setStopQuantity(params.get(i).getStopNumber());
                segment.setFlightNumber(params.get(i).getFlightNo());
                segment.setMarketingAirline("MJ");
                segment.setEquipment(params.get(i).getEquipment());
                segment.setFareReference(params.get(i).getCabinCode()+"ST"+params.get(i).getTravlType());
                currency = params.get(i).getCurrency();
                airline = params.get(i).getAirline();
                vector.add(segment);
            }
            pricingRequest.setTicketingCarrier(airline);
            pricingRequest.setCurrencyCode(currency);
            pricingRequest.setFlightSegments(vector);
            PricingResult result = pricing.doPricing(pricingRequest);
           /* System.out.println("===============pricingRequest================" + JSON.toJSONString(pricingRequest) + "===============================");
            System.out.println("===============pricingResponse================" + JSON.toJSONString(result) + "===============================");*/
            List<TicketPrice> ticketPrices = getTicketPrices(result,currency);
            for (TicketPrice ticketPrice:ticketPrices) {
                ticketPrice.setRequestStr(JSON.toJSONString(pricingRequest));
                ticketPrice.setResponseStr(JSON.toJSONString(result));
            }
            return ticketPrices;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<TicketPrice> getTicketPrices(PricingResult result, String currency){
        List<TicketPrice> ticketPrices = new ArrayList<TicketPrice>();
        for (int i = 0; i < result.getPricedItinerarys().size(); i++){
            TicketPrice ticketPrice = new TicketPrice();
            PIFlightSegment piFlightSegment = (PIFlightSegment)result.getPricedItineraryAt(i).getFlightSegments().get(0);
            PIFlightSegment piFlightSegment1 = null;
            if(result.getPricedItineraryAt(i).getFlightSegments().size() == 2){
                piFlightSegment1 = (PIFlightSegment)result.getPricedItineraryAt(i).getFlightSegments().get(1);
                ticketPrice.setCabinType((piFlightSegment.getResBookDesigCode() + "-" + piFlightSegment1.getResBookDesigCode()).trim());
            }else{
                ticketPrice.setCabinType(piFlightSegment.getResBookDesigCode());
            }
            PIPricedItinerary piPricedItinerary = result.getPricedItineraryAt(i);
            ticketPrice.setFuncCtrlChar(piPricedItinerary.getFuncCtrlChar());
            ticketPrice.setTotalFareAmount(piPricedItinerary.getFareBreakDownAt(0).getTotalFare_Amount());
            ticketPrice.setTotalFareCurrencyCode(piPricedItinerary.getFareBreakDownAt(0).getTotalFare_CurrencyCode());
            ticketPrice.setBaseFareAmount(piPricedItinerary.getFareBreakDownAt(0).getBaseFare_Amount());
            ticketPrice.setBaseFareCurrencyCode(piPricedItinerary.getFareBreakDownAt(0).getBaseFare_CurrencyCode());
            //基本运价的币种
            //如果基础币种和总价币种一致，要将等同货币补全为基础币种的价格和币种
            if(piPricedItinerary.getFareBreakDownAt(0).getBaseFare_CurrencyCode()
                    .equals(piPricedItinerary.getFareBreakDownAt(0).getTotalFare_CurrencyCode())){
                ticketPrice.setEquivFareCurrentyCode(piPricedItinerary.getFareBreakDownAt(0).getBaseFare_CurrencyCode());
                ticketPrice.setEquivFareAmount(piPricedItinerary.getFareBreakDownAt(0).getBaseFare_Amount());
            }else{
                //如果不同，则不用补全
                ticketPrice.setEquivFareCurrentyCode(piPricedItinerary.getFareBreakDownAt(0).getEquivFare_CurrencyCode());
                ticketPrice.setEquivFareAmount(piPricedItinerary.getFareBreakDownAt(0).getEquivFare_Amount());
            }
            ticketPrice.setBaseFareRate(piPricedItinerary.getFareBreakDownAt(0).getBaseFare_Rate());
            Vector<PITaxBreakDown> vector = piPricedItinerary.getFareBreakDownAt(0).getTaxBreakDowns();
            List<TaxPrice> taxPrices = new ArrayList<TaxPrice>();
            for(int j = 0; j<vector.size(); j++){
                TaxPrice taxPrice = new TaxPrice();
                PITaxBreakDown piTaxBreakDown = vector.get(j);
                taxPrice.setEXEMPT(piTaxBreakDown.isEXEMPT());
                taxPrice.setTaxName(piTaxBreakDown.getTaxName());
                taxPrice.setTaxCode(piTaxBreakDown.getTaxCode());
                taxPrice.setCurrencyCode(piTaxBreakDown.getCurrencyCode());
                taxPrice.setAmount(piTaxBreakDown.getAmount());
                taxPrice.setCountryCode(piTaxBreakDown.getCountryCode());
                taxPrices.add(taxPrice);
            }
            ticketPrice.setTaxPrices(taxPrices);
            ticketPrices.add(ticketPrice);
        }
//        TicketPrice ticketPrice = new TicketPrice();
//        PIFlightSegment piFlightSegment = (PIFlightSegment)result.getPricedItineraryAt(0).getFlightSegments().get(0);
//        PIFlightSegment piFlightSegment1 = null;
//        if(result.getPricedItineraryAt(0).getFlightSegments().size() == 2){
//            piFlightSegment1 = (PIFlightSegment)result.getPricedItineraryAt(0).getFlightSegments().get(1);
//            ticketPrice.setCabinType((piFlightSegment.getResBookDesigCode() + "-" + piFlightSegment1.getResBookDesigCode()).trim());
//        }else{
//            ticketPrice.setCabinType(piFlightSegment.getResBookDesigCode());
//        }
//        PIPricedItinerary piPricedItinerary = result.getPricedItineraryAt(0);
//        ticketPrice.setTotalFareAmount(piPricedItinerary.getFareBreakDownAt(0).getTotalFare_Amount());
//        ticketPrice.setTotalFareCurrencyCode(piPricedItinerary.getFareBreakDownAt(0).getTotalFare_CurrencyCode());
//        ticketPrice.setBaseFareAmount(piPricedItinerary.getFareBreakDownAt(0).getBaseFare_Amount());
//        ticketPrice.setBaseFareCurrencyCode(piPricedItinerary.getFareBreakDownAt(0).getBaseFare_CurrencyCode());
//        //基本运价的币种
//        //如果基础币种和总价币种一致，要将等同货币补全为基础币种的价格和币种
//        if(piPricedItinerary.getFareBreakDownAt(0).getBaseFare_CurrencyCode()
//                .equals(piPricedItinerary.getFareBreakDownAt(0).getTotalFare_CurrencyCode())){
//            ticketPrice.setEquivFareCurrentyCode(piPricedItinerary.getFareBreakDownAt(0).getBaseFare_CurrencyCode());
//            ticketPrice.setEquivFareAmount(piPricedItinerary.getFareBreakDownAt(0).getBaseFare_Amount());
//        }else{
//            //如果不同，则不用补全
//            ticketPrice.setEquivFareCurrentyCode(piPricedItinerary.getFareBreakDownAt(0).getEquivFare_CurrencyCode());
//            ticketPrice.setEquivFareAmount(piPricedItinerary.getFareBreakDownAt(0).getEquivFare_Amount());
//        }
//        ticketPrice.setBaseFareRate(piPricedItinerary.getFareBreakDownAt(0).getBaseFare_Rate());
//        Vector<PITaxBreakDown> vector = piPricedItinerary.getFareBreakDownAt(0).getTaxBreakDowns();
//        List<TaxPrice> taxPrices = new ArrayList<TaxPrice>();
//        for(int j = 0; j<vector.size(); j++){
//            TaxPrice taxPrice = new TaxPrice();
//            PITaxBreakDown piTaxBreakDown = vector.get(j);
//            taxPrice.setEXEMPT(piTaxBreakDown.isEXEMPT());
//            taxPrice.setTaxName(piTaxBreakDown.getTaxName());
//            taxPrice.setTaxCode(piTaxBreakDown.getTaxCode());
//            taxPrice.setCurrencyCode(piTaxBreakDown.getCurrencyCode());
//            taxPrice.setAmount(piTaxBreakDown.getAmount());
//            taxPrice.setCountryCode(piTaxBreakDown.getCountryCode());
//            taxPrices.add(taxPrice);
//        }
//        ticketPrice.setTaxPrices(taxPrices);
        return ticketPrices;

    }
}
